package com.example.androidframework.ui.mall

import android.view.View
import androidx.lifecycle.viewModelScope
import com.example.androidframework.ui.mall.bean.MallGoodsTypeBean
import com.example.androidframework.ui.mall.holder.HotGoodsViewHolder
import com.example.androidframework.ui.mall.holder.MallGoodsTypeViewHolder
import com.example.androidframework.ui.mall.holder.ScoreGoodsViewHolder
import com.martin.lib_base.base.BaseViewModel
import com.martin.lib_base.bean.app.mall.HomeMallBean
import com.martin.lib_base.ktx_live.buildRequest
import com.martin.lib_base.ktx_live.launchLoadingCollect
import com.martin.lib_base.net.NetApply
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

class MallViewModel : BaseViewModel() {
    ///////////////////////////////////////////////////////////////////////////
    // ------------------------------ 属性区 -------------------------------- //
    ///////////////////////////////////////////////////////////////////////////

    var p = 1
    var isLoadMore = false

    // 数据
    var mallData = MutableStateFlow<HomeMallBean>(HomeMallBean())

    // 消息数量
    var messageCount = MutableStateFlow(0)

    // 商品分类
    val typeViewHolder = listOf(
        MallGoodsTypeViewHolder::class.java
    )

    // 积分专区
    val scoreViewHolder = listOf(
        ScoreGoodsViewHolder::class.java
    )

    // 精选热卖
    val hotViewHolder = listOf(
        HotGoodsViewHolder::class.java
    )

    ///////////////////////////////////////////////////////////////////////////
    // -------------------------------方法区 -------------------------------- //
    ///////////////////////////////////////////////////////////////////////////

    fun requestData() {
        launchLoadingCollect({
            NetApply.api.getShopList(
                buildRequest(
                    "Home.GetShopList",
                    mapOf(
                        "p" to p
                    )
                )
            )
        }) {
            onSuccess = {
                it.info?.let {
                    mallData.value = it
                }
            }
        }
    }

    // 修改消息数量
    fun postMessageCount(count: Int) {
        messageCount.value = count
    }

    // 监听消息数量
    fun observeMessageCount(scope: CoroutineScope? = null, method: (Int) -> Unit = {_ -> }) {
        val mScope = scope ?: viewModelScope
        mScope.launch {
            messageCount.collect {
                method.invoke(it)
            }
        }
    }

    // 点击消息图标
    fun messageClick(view: View) {

    }

}